Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

recast

Package Overview
Dependencies
Maintainers
1
Versions
266
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

recast

JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator

  • 0.13.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11M
decreased by-12.6%
Maintainers
1
Weekly downloads
 
Created

What is recast?

The recast npm package is a JavaScript library for parsing, transforming, and printing JavaScript code. It allows developers to manipulate the syntax tree of JavaScript code, enabling tasks such as code refactoring, code generation, and more. Recast preserves the original formatting of the code as much as possible, which is useful when modifying existing code.

What are recast's main functionalities?

Parsing JavaScript code into an Abstract Syntax Tree (AST)

This feature allows you to parse a string of JavaScript code into an AST, which can then be manipulated or analyzed.

const recast = require('recast');
const code = 'let x = 42;';
const ast = recast.parse(code);

Transforming the AST

This feature enables you to traverse and modify the AST. In this example, all 'let' declarations are changed to 'var'.

const recast = require('recast');
const ast = recast.parse('let x = 42;');
recast.types.visit(ast, {
  visitVariableDeclaration(path) {
    path.node.kind = 'var';
    return false;
  }
});
const transformedCode = recast.print(ast).code;

Printing the modified AST back to JavaScript code

After modifying the AST, this feature allows you to print it back into a formatted JavaScript code string.

const recast = require('recast');
const ast = recast.parse('let x = 42;');
// ... modify the AST ...
const modifiedCode = recast.print(ast).code;

Other packages similar to recast

Keywords

FAQs

Package last updated on 18 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc